import os.path
import random
import time

import requests
from lxml import etree
from urllib import request as req

def main(url):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36'
    }

    resp = requests.get(url, headers=headers)
    resp.encoding = 'GBK'
    # print(resp.text)

    tree = etree.HTML(resp.text)
    li = tree.xpath('//ul[@class="clearfix"]/li')
    for i in li:
        # 获取图片url
        href = i.xpath('./a//img/@src')
        # 拼凑完整url
        image_url = 'https://pic.netbian.com' + href[0]
        image_name = i.xpath('./a/b/text()')[0]

        # print(image_url)
        path = '../img'
        if not os.path.exists(path):
            os.mkdir(path)
        req.urlretrieve(image_url, os.path.join(path, image_name+'.jpg'))
        print(image_name)
        time.sleep(random.randint(1,3))



if __name__=='__main__':
    url = "https://pic.netbian.com/4k/"
    main(url)